package ru.dadle8.service.impl.bot;

import org.springframework.stereotype.Service;
import ru.dadle8.model.bot.entity.MenuAction;
import ru.dadle8.model.bot.entity.TrainingMenuAction;
import ru.dadle8.service.api.bot.MenuActionService;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

@Service
public class MenuActionServiceImpl implements MenuActionService {

    @Override
    public MenuAction getMenuAction(String text) {
        Collection<MenuAction> menuActions = getMenuActions();
        menuActions.addAll(getMenuActionsByDefault());
        for (MenuAction menuAction : menuActions) {
            MenuAction result = getMenuAction(menuAction, text);
            if (result != null) {
                return result;
            }
        }
        return null;
    }

    @Override
    public MenuAction getMenuAction(int actionId) {
        Collection<MenuAction> menuActions = getMenuActions();
        menuActions.addAll(getMenuActionsByDefault());
        for (MenuAction menuAction : menuActions) {
            MenuAction result = getMenuAction(menuAction, actionId);
            if (result != null) {
                return result;
            }
        }
        return null;
    }

    @Override
    public MenuAction getMenuAction(MenuAction menuAction, int actionId) {
        if (actionId == menuAction.getActionId()) {
            return menuAction;
        }
        for (MenuAction childMenuAction : menuAction.getChildActions()) {
            MenuAction result = getMenuAction(childMenuAction, actionId);
            if (result != null) {
                return result;
            }
        }
        return null;
    }

    @Override
    public MenuAction getMenuAction(MenuAction menuAction, String text) {
        if (text.equalsIgnoreCase(menuAction.getTitle())) {
            return menuAction;
        }
        for (MenuAction childMenuAction : menuAction.getChildActions()) {
            MenuAction result = getMenuAction(childMenuAction, text);
            if (result != null) {
                return result;
            }
        }
        return null;
    }

    @Override
    public Collection<MenuAction> getMenuActions() {
        Set<MenuAction> menuActionList = new HashSet<>();

        ////

        MenuAction menuActionForHome = new MenuAction();
        menuActionForHome.setTitle("Тренировки для дома");
        menuActionForHome.setActionId(1);
        menuActionForHome.setOrder(1);
        menuActionForHome.setRowOrder(1);

        // Child actions
        MenuAction functions = new MenuAction();
        functions.setTitle("Функциональные");
        functions.setActionId(2);
        functions.setParentActionId(1);
        functions.setOrder(1);
        functions.setRowOrder(1);
        menuActionForHome.addChildAction(functions);

        // Child training actions
        TrainingMenuAction firstTraining = new TrainingMenuAction();
        firstTraining.setTitle("Первая тренировочка");
        firstTraining.setActionId(12);
        firstTraining.setParentActionId(2);
        firstTraining.setOrder(1);
        firstTraining.setRowOrder(1);
        functions.addChildAction(firstTraining);

        MenuAction fromSedentaryLife = new MenuAction();
        fromSedentaryLife.setTitle("От сидячей жизни");
        fromSedentaryLife.setActionId(3);
        fromSedentaryLife.setParentActionId(1);
        fromSedentaryLife.setOrder(2);
        fromSedentaryLife.setRowOrder(1);
        menuActionForHome.addChildAction(fromSedentaryLife);

        MenuAction lowerBackHurts = new MenuAction();
        lowerBackHurts.setTitle("Болит поясница");
        lowerBackHurts.setActionId(4);
        lowerBackHurts.setParentActionId(1);
        lowerBackHurts.setOrder(1);
        lowerBackHurts.setRowOrder(2);
        menuActionForHome.addChildAction(lowerBackHurts);

        MenuAction upperBackHurts = new MenuAction();
        upperBackHurts.setTitle("Болит верх спины");
        upperBackHurts.setActionId(5);
        upperBackHurts.setParentActionId(1);
        upperBackHurts.setOrder(2);
        upperBackHurts.setRowOrder(2);
        menuActionForHome.addChildAction(upperBackHurts);

        menuActionList.add(menuActionForHome);

        ////

        MenuAction menuActionForGym = new MenuAction();
        menuActionForGym.setTitle("Тренировки для зала");
        menuActionForGym.setActionId(6);
        menuActionForGym.setOrder(2);
        menuActionForGym.setRowOrder(1);

        // Child actions
        MenuAction gymFunctions = new MenuAction();
        gymFunctions.setTitle("Функциональные для зала");
        gymFunctions.setActionId(7);
        gymFunctions.setParentActionId(6);
        gymFunctions.setOrder(1);
        gymFunctions.setRowOrder(1);
        menuActionForGym.addChildAction(gymFunctions);

        MenuAction hypertrophyVolume = new MenuAction();
        hypertrophyVolume.setTitle("Гипертрофия (объем)");
        hypertrophyVolume.setActionId(8);
        hypertrophyVolume.setParentActionId(6);
        hypertrophyVolume.setOrder(2);
        hypertrophyVolume.setRowOrder(1);
        menuActionForGym.addChildAction(hypertrophyVolume);

        // Child training actions
        TrainingMenuAction hypertrophyVolumeTraining1 = new TrainingMenuAction();
        hypertrophyVolumeTraining1.setTitle("Тренировка на плечи");
        hypertrophyVolumeTraining1.setActionId(7000);
        hypertrophyVolumeTraining1.setParentActionId(8);
        hypertrophyVolumeTraining1.setOrder(1);
        hypertrophyVolumeTraining1.setRowOrder(1);
        hypertrophyVolume.addChildAction(hypertrophyVolumeTraining1);

        MenuAction hypertrophyIntensity = new MenuAction();
        hypertrophyIntensity.setTitle("Гипертрофия (интенсивность)");
        hypertrophyIntensity.setActionId(9);
        hypertrophyIntensity.setParentActionId(6);
        hypertrophyIntensity.setOrder(1);
        hypertrophyIntensity.setRowOrder(2);
        menuActionForGym.addChildAction(hypertrophyIntensity);

        menuActionList.add(menuActionForGym);

        ////

        MenuAction menuActionStretch = new MenuAction();
        menuActionStretch.setTitle("Растяжка");
        menuActionStretch.setActionId(2000);
        menuActionStretch.setOrder(1);
        menuActionStretch.setRowOrder(2);
        menuActionList.add(menuActionStretch);

        TrainingMenuAction stretch1 = new TrainingMenuAction();
        stretch1.setTitle("Растяжка на все тело");
        stretch1.setActionId(2001);
        stretch1.setParentActionId(2000);
        stretch1.setOrder(1);
        stretch1.setRowOrder(1);
        stretch1.setCount(1);
        menuActionStretch.addChildAction(stretch1);

        TrainingMenuAction stretch2 = new TrainingMenuAction();
        stretch2.setTitle("Мобильность коленного сустава");
        stretch2.setActionId(2002);
        stretch2.setParentActionId(2000);
        stretch2.setOrder(2);
        stretch2.setRowOrder(1);
        stretch2.setCount(1);
        menuActionStretch.addChildAction(stretch2);

        TrainingMenuAction stretch3 = new TrainingMenuAction();
        stretch3.setTitle("Разминка тазобедренного сустава");
        stretch3.setActionId(2003);
        stretch3.setParentActionId(2000);
        stretch3.setOrder(1);
        stretch3.setRowOrder(2);
        stretch3.setCount(1);
        menuActionStretch.addChildAction(stretch3);

        TrainingMenuAction stretch4 = new TrainingMenuAction();
        stretch4.setTitle("Разминка коленного сустава");
        stretch4.setActionId(2004);
        stretch4.setParentActionId(2000);
        stretch4.setOrder(2);
        stretch4.setRowOrder(2);
        stretch4.setCount(1);
        menuActionStretch.addChildAction(stretch4);

        ////

        MenuAction menuActionMRF = new MenuAction();
        menuActionMRF.setTitle("МФР");
        menuActionMRF.setActionId(11);
        menuActionMRF.setOrder(2);
        menuActionMRF.setRowOrder(2);
        menuActionList.add(menuActionMRF);

        return menuActionList;
    }

    @Override
    public Collection<MenuAction> getMenuActionsByDefault() {
        Set<MenuAction> menuActionList = new HashSet<>();

        ////

        MenuAction menuActionBack = new MenuAction();
        menuActionBack.setTitle("Назад");
        menuActionBack.setActionId(100);
        menuActionBack.setOrder(100);

        menuActionList.add(menuActionBack);

        return menuActionList;
    }
}
